package com.hzo.great.leisuregreat.adapters;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzo.great.leisuregreat.R;
import com.hzo.great.leisuregreat.beans.PhoneBean;

import java.util.List;

public class PhoneListAdapter extends RecyclerView.Adapter<PhoneListAdapter.MyViewHolder> {
    private List<PhoneBean> list;//存放数据
    private Context mContext;

    PhoneListAdapter(List<PhoneBean> list, Context context) {
        this.list = list;
        this.mContext = context;
    }

    @NonNull
    @Override
    public PhoneListAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PhoneListAdapter.MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.home_list_phone_item, parent, false));
    }

    //在这里可以获得每个子项里面的控件的实例，比如这里的TextView,子项本身的实例是itemView，
    // 在这里对获取对象进行操作
    //holder.itemView是子项视图的实例，holder.textView是子项内控件的实例
    //position是点击位置
    @Override
    public void onBindViewHolder(@NonNull PhoneListAdapter.MyViewHolder holder, int position) {
        PhoneBean phoneBean = list.get(position);
        if (phoneBean.isIsTested()) {
            holder.img_home_item_check.setVisibility(View.VISIBLE);
        } else {
            holder.img_home_item_check.setVisibility(View.GONE);
        }
        Glide.with(mContext).asBitmap().load(phoneBean.getIcon()).into(holder.img_home_item_phone);
        holder.txt_home_item_model.setText(phoneBean.getProductName());
        holder.txt_home_item_memory.setText(phoneBean.getMemory());
        holder.txt_home_item_degree.setText(String.valueOf(phoneBean.getDegree() / 10 + "成新"));
        holder.txt_home_item_new_price.setText(mContext.getString(R.string.save_money_msg, String.valueOf(phoneBean.getNowPrice())));
        holder.txt_home_item_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);//设置中划线并加清晰
        holder.txt_home_item_old_price.setText("市场价" + String.valueOf(phoneBean.getOriginalPrice()));
        holder.txt_home_item_save_money.setText(mContext.getString(R.string.save_money_msg, String.valueOf((phoneBean.getOriginalPrice() - phoneBean.getNowPrice()))));
    }

    //要显示的子项数量
    @Override
    public int getItemCount() {
        return list.size();
    }

    //这里定义的是子项的类，不要在这里直接对获取对象进行操作
    class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView img_home_item_check;
        ImageView img_home_item_phone;
        TextView txt_home_item_model;
        TextView txt_home_item_memory;
        TextView txt_home_item_degree;
        TextView txt_home_item_new_price;
        TextView txt_home_item_old_price;
        TextView txt_home_item_save_money;

        MyViewHolder(View itemView) {
            super(itemView);
            img_home_item_check = itemView.findViewById(R.id.img_home_item_check);
            img_home_item_phone = itemView.findViewById(R.id.img_home_item_phone);
            txt_home_item_model = itemView.findViewById(R.id.txt_home_item_model);
            txt_home_item_memory = itemView.findViewById(R.id.txt_home_item_memory);
            txt_home_item_degree = itemView.findViewById(R.id.txt_home_item_degree);
            txt_home_item_new_price = itemView.findViewById(R.id.txt_home_item_new_price);
            txt_home_item_old_price = itemView.findViewById(R.id.txt_home_item_old_price);
            txt_home_item_save_money = itemView.findViewById(R.id.txt_home_item_save_money);
        }
    }

    /*之下的方法都是为了方便操作，并不是必须的*/

    //在指定位置插入，原位置的向后移动一格
    public boolean addItem(int position, PhoneBean bean) {
        if (position < list.size() && position >= 0) {
            list.add(position, bean);
            notifyItemInserted(position);
            return true;
        }
        return false;
    }

    //去除指定位置的子项
    public boolean removeItem(int position) {
        if (position < list.size() && position >= 0) {
            list.remove(position);
            notifyItemRemoved(position);
            return true;
        }
        return false;
    }

    //清空显示数据
    public void clearAll() {
        list.clear();
        notifyDataSetChanged();
    }
}
